# -*- coding: utf-8 -*-
# @Time : 2022/2/7 11:47

from src.ApiTest.ProjectConfig.Database.mysqlConfig_database import MysqlConfig, MysqlConfigSchema
from src.User.Database.user_database import User, UserSchema
from Common.yaml_method import YamlMethod
import ast
from sqlalchemy import and_


class MysqlList:
    """
    获取数据库配置列表接口
    """

    @staticmethod
    def mysql_list(page, limit, config_name, user_id, project_id):
        """
        获取数据库配置列表接口
        :param page: 页码
        :param limit: 每页多少条数据
        :param config_name: 配置名称
        :param user_id: user_id
        :param project_id: 项目ID
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        if project_id:
            if config_name:
                # projectId和configName都不为空
                data = MysqlConfig.query.filter_by(configName=config_name, projectId=project_id)\
                    .paginate(page=page, per_page=limit)
            else:
                # projectId不为空，configName都为空
                data = MysqlConfig.query.filter_by(projectId=project_id).paginate(page=page, per_page=limit)
        else:
            # 先查询用户所属项目信息
            user = User.query.filter_by(id=user_id).first()
            user_schema = UserSchema()
            user_data = user_schema.dump(user)
            user_project_id = user_data['user_projects']
            id_list = ast.literal_eval(user_project_id)
            # 开始构建过滤条件
            filters = [MysqlConfig.projectId.in_(id_list)]
            if config_name:
                # projectId为空，configName不为空
                filters.append(MysqlConfig.configName == config_name)
                data = MysqlConfig.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
            else:
                # projectId和configName都为空
                data = MysqlConfig.query.filter(MysqlConfig.projectId.in_(id_list)).paginate(page=page, per_page=limit)

        info = []
        if data:
            for i in data.items:
                mysql_schema = MysqlConfigSchema()
                mysql_data = mysql_schema.dump(i)
                # 将单条信息添加到info中
                info.append(mysql_data)

        total = data.total
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'mysqlList': info,
                'total': total
            }
        }

        return res

    @staticmethod
    def mysql_info(project_name, environment, db_type):
        """
        获取数据库配置信息接口
        :param project_name: 项目名称
        :param environment: 配置名称
        :param db_type: 数据库类型
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        data = MysqlConfig.query.filter_by(projectName=project_name, environment=environment, databaseType=db_type)\
            .first()
        mysql_schema = MysqlConfigSchema()
        mysql_data = mysql_schema.dump(data)

        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'mysqlInfo': mysql_data
            }
        }

        return res
